-- Operative main include file

-- Constants
GAME_WIDTH = 1600.0
GAME_HEIGHT = 1024.0
INVALID_RADIUS = 50.0

ITEM_TYPE_NONE = 0
ITEM_TYPE_PERSON = 1
ITEM_TYPE_WEAPON = 2
ITEM_TYPE_BULLET = 3
ITEM_TYPE_ANIMATED = 4
ITEM_TYPE_BODY = 5
ITEM_TYPE_BACKGROUND = 6

AI_GUARD = 0
AI_GUARD_ALERT = 1
AI_PATROL = 2

PLACE_HOLDER_NONE = 0
PLACE_HOLDER_PLAYER = 1
PLACE_HOLDER_GUARD = 2
PLACE_HOLDER_ALERT_GUARD = 3
PLACE_HOLDER_PATROL = 4
PLACE_HOLDER_WEAPON = 5
PLACE_HOLDER_MAX = 5

BULLET_TYPE_NORMAL = 0
BULLET_TYPE_PROJECTILE = 1

SHAPE_TYPE_LINE = 1
SHAPE_TYPE_IMAGE = 2
SHAPE_TYPE_CLIP = 3

Z_BACKGROUND = 0
Z_FOREGROUND = 1

PLAYER_INPUT_RIGHT = 1
PLAYER_INPUT_LEFT = 2
PLAYER_INPUT_UP = 4
PLAYER_INPUT_DOWN = 8
PLAYER_INPUT_FIRE = 16
WEAPON_INPUT_FIRE = 1
WEAPON_INPUT_RIGHT = 0
WEAPON_INPUT_LEFT = 2
WEAPON_INPUT_DIRECTION = 2

REACH_DISTANCE = 200.0

VIEW_DISTANCE = 800
VIEW_DIVISIONS = 36
GROUND_TEST_DISTANCE = 50
HIT_TEST_DISTANCE = 30
LOOK_CHANGE_TIME = 3000

TEAM_NEUTRAL = 0
TEAM_PLAYER = 1
TEAM_ENEMY = 2

PLAYER_STATE_DIRECTION = 1
PLAYER_STATE_RIGHT = 0
PLAYER_STATE_LEFT = 1
PLAYER_STATE_LOOK_DIRECTION = 2
PLAYER_STATE_LOOK_RIGHT = 0
PLAYER_STATE_LOOK_LEFT = 2
PLAYER_STATE_MOVING = 4
PLAYER_STATE_FLYING = 8
PLAYER_MOVE_THRESHOLD = 100.0

MAX_ANIMATIONS = 20
MAX_FRAMES = 100
MAX_CLIPS = 500

CLIP_STOPPED = 0
CLIP_PLAYING = 1
CLIP_LOOPING = 2

CLIP_CURRENT_FRAME = -1
CLIP_FIRST_FRAME = 0
CLIP_LAST_FRAME = -2

ID_NONE = 0
ID_ITEM = 1
ID_BODY = 2
ID_FIXTURE = 3
ID_SHAPE = 4

CARET_BLINK_RATE = 600

LISTBOX_ITEM_HEIGHT = 24
SCROLLBAR_SIZE = 24

MAX_SOUND_INSTANCES = 3

MODE_MENU = 0
MODE_NEWCAMPAIGN = 1
MODE_LOADCAMPAIGN = 2
MODE_PLAYLEVEL = 3
MODE_EDITLEVEL = 4
MODE_OPTIONS = 5
MODE_QUIT = 6

-- Interface layer

__item = {
	["__index"] = ScriptGetItemField,
	["__newindex"] = ScriptSetItemField,
	["__eq"] = function (a, b)
		return a["itemId"] == b["itemId"]
	end
}

function items(itemId)
	if type(itemId) == "string" then
		itemId = dbGetItemFromName(itemId)
	end
	
	return setmetatable({ ["itemId"] = itemId }, __item)
end

__weaponInfo = {
	["__index"] = ScriptGetWeaponInfoField,
	["__newindex"] = ScriptSetWeaponInfoField,
	["__eq"] = function (a, b)
		return a["weaponId"] == b["weaponId"]
	end
}

function weapons(weaponId)
	if type(weaponId) == "string" then
		weaponId = dbGetWeaponId(weaponId)
	end
	
	return setmetatable({ ["weaponId"] = weaponId }, __weaponInfo)
end

__bulletInfo = {
	["__index"] = ScriptGetBulletInfoField,
	["__newindex"] = ScriptSetBulletInfoField,
	["__eq"] = function (a, b)
		return a["bulletId"] == b["bulletId"]
	end
}

function bullets(bulletId)
	return setmetatable({ ["bulletId"] = bulletId }, __bulletInfo)
end

function ForEachItem(f)
	n = dbGetItemCount()
	for i = 0, n do
		item = items(i)
		if item.used then
			f(item)
		end
	end
end

function DebugOutput(v)
	file = io.open("script_debug.txt", "a")
	file:write(v, "\n");
	io.close(file)
end

function Argb(a, r, g, b)
	if a >= 0x80 then
		return (a-0x100)*0x01000000 + r*0x00010000 + g*0x00000100 + b
	else
		return a*0x01000000 + r*0x00010000 + g*0x00000100 + b
	end
end
